package com.mengze.linlee_iot_adnroid.ui.ucenter

import android.app.AlertDialog
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.mengze.linlee_iot_adnroid.databinding.FragmentMeBinding

class UcenterFragment : Fragment() {

    private val menuItems = listOf(
        "个人信息",
        "设备信息",
        "用户反馈",
        "当前版本",
//
//        ProfileItem("我的订单", R.drawable.ic_order),
//        ProfileItem("收藏店铺", R.drawable.ic_favorite),
//        ProfileItem("收货地址", R.drawable.ic_address),
//        ProfileItem("优惠券", R.drawable.ic_coupon),
//        ProfileItem("客服中心", R.drawable.ic_service),
//        ProfileItem("设置", R.drawable.ic_settings),
//        ProfileItem("退出登录", R.drawable.ic_logout)
    )

    private lateinit var ucenterViewModel: UcenterViewModel
    private var _binding: FragmentMeBinding? = null
    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {


        val activity = getActivity();
        // 方法1：直接设置颜色值
        activity?.window?.statusBarColor = Color.parseColor("#FFFFFFFF") // 橙色
// 方法2：使用资源颜色（推荐）
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            activity?.window?.statusBarColor = ContextCompat.getColor(activity.this, R.color.purple_200)
        }
        activity?.window.apply {
            var statusBarColor = Color.parseColor("#FFFFFFFF") // 设置颜色
        }


        ucenterViewModel =
            ViewModelProvider(this).get(UcenterViewModel::class.java)

        _binding = FragmentMeBinding.inflate(inflater, container, false)
        val root: View = binding.root

        binding.tvUsername.text = "外卖用户"
        binding.tvPhone.text = "138****8888"
        binding.tvType.text = "全职"
        return root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        println("onViewCreated~~~~")
        setupViews()
    }


    private fun setupViews() {
//        _binding?.ivAvatar?.setImageResource(R.drawable.ic_default_avatar)
        binding?.rvMenu?.layoutManager = LinearLayoutManager(requireContext())
        binding?.rvMenu?.adapter = ProfileAdapter(menuItems) { item ->

        }

        binding.logoutButton.setOnClickListener( object: View.OnClickListener {
            override fun onClick(p0: View?) {
                showLogoutDialog()
            }
        })
    }


    private fun showLogoutDialog() {
        AlertDialog.Builder(requireContext())
            .setTitle("提示")
            .setMessage("确定要退出吗？")
            .setPositiveButton("确定") { _, _ ->
                // 执行退出登录逻辑

//                activity?.finish()
            }
            .setNegativeButton("取消", null)
            .show()
    }



    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}